import { Component, OnInit } from '@angular/core';
import { DialogService } from 'primeng/primeng';
import { ReportConfigService } from './report-config.service';
import { ReportService } from '../report.service';
import { ReportConfigCreateComponent } from './base/report-config-create.component';

@Component({
    templateUrl: 'report-config.component.html',
    styleUrls: ['report-config.component.scss']
})
export class ReportConfigComponent implements OnInit {
    reportTree: any[] = [];
    selectedNode: any;
    reportId: number = 0;
    report: any = {};
    constructor(
        private dialogService: DialogService,
        private reportConfigService: ReportConfigService,
        private reportService: ReportService
    ) {
    }
    public ngOnInit() {
        this.loadTree();
        this.reportConfigService.routeMsgPush$.subscribe(data=>this.loadTree());
    }

    loadTree(){
        this.reportConfigService.loadReportTree().subscribe(data => {
            if(data.length > 0){
                data[0].expanded = true;
                data[0].children[0].expanded = true;
                this.selectedNode = data[0].children[0].children[0];
                this.reportTree = data;
                this.nodeSelect();
            }
        });
    }
    /**节点选中 */
    nodeSelect() {
        this.reportId = +this.selectedNode['id'];
        this.reportService.loadReport(this.reportId).subscribe(data => {
            this.report = data;
        });
    }

    /** 创建查询统计 */
    createQuery() {
        const ref = this.dialogService.open(ReportConfigCreateComponent, {
            'header': '新增报表',
            'width': '500px',
            contentStyle: { "height": "450px" }
        });
        ref.onClose.subscribe((data: any) => {
            if (data) {
                this.reportConfigService.loadReportTree().subscribe(data => this.reportTree = data);
            }
        });
    }
}

